package ioperation;

import book.Book;
import book.BookList;

import java.util.Scanner;

public class DeleteOperation implements IOPeration{
    @Override
    public void work(BookList bookList) {
        System.out.println("图书删除");

        Scanner scanner=new Scanner(System.in);
        System.out.println("输入你想删除的书名");
        String name=scanner.nextLine();

        int currentSize=bookList.getUsedSize();

        int i=0;
        //找到这本书
        int pos=-1;//记录要删除图书的位置
        for(;i<currentSize;i++){
            Book book=bookList.getBook(i);
            if(book.getName().equals(name)){
                pos=i;//i为要删除图书的位置
                break;
            }
        }
        //判断有无删除的书
        //因为数字下标从0开始
        if(i==currentSize){
            System.out.println("没有你要删除的书");
            return;
        }
        //删除操作
        int j=pos;
        for(;j<currentSize-1;j++){
            Book book=bookList.getBook(j+1);
            bookList.setBook(j,book);
        }
        bookList.setUsedSize(currentSize-1);
        System.out.println("删除成功");

    }
}
